package cn.micro.system.manage.service.impl;

import cn.micro.system.manage.entity.SysDimRole;
import cn.micro.system.manage.entity.SysDimUserRole;
import cn.micro.system.manage.entity.vo.AssginRoleVo;
import cn.micro.system.manage.entity.vo.SysRoleQueryVo;
import cn.micro.system.manage.mapper.SysDimRoleMapper;
import cn.micro.system.manage.mapper.SysDimUserRoleMapper;
import cn.micro.system.manage.service.SysDimRoleService;
import cn.micro.system.manage.service.SysDimUserRoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

/**
 * <p>
 * 角色信息表 服务实现类
 * </p>
 *
 * @author wr
 * @since 2023-12-25
 */
@Service
public class SysDimRoleServiceImpl extends ServiceImpl<SysDimRoleMapper, SysDimRole> implements SysDimRoleService {

    @Resource
    private SysDimUserRoleMapper sysDimUserRoleMapper;

    //条件分页查询
    @Override
    public List<SysDimRole> selectPage(int page,int size, SysRoleQueryVo sysRoleQueryVo) {
        PageHelper.startPage(page,size);
        return baseMapper.selectPage(sysRoleQueryVo);
    }

    //获取用户的角色数据
    @Override
    public Map<String, Object> getRolesByUserId(String userId) {
        //获取所有角色
        List<SysDimRole> roles = baseMapper.selectList(null);
        //根据用户id查询，已经分配角色
        QueryWrapper<SysDimUserRole> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<SysDimUserRole> userRolesList = sysDimUserRoleMapper.selectList(wrapper);
        //从userRoles集合获取所有角色id
        List<String> userRoleIds = new ArrayList<>();
        for (SysDimUserRole userRole:userRolesList) {
            String roleId = String.valueOf(userRole.getRoleId());
            userRoleIds.add(roleId);
        }
        //封装到map集合
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("allRoles",roles);//所有角色
        returnMap.put("userRoleIds",userRoleIds);//用户分配角色id集合
        return returnMap;
    }

    //用户分配角色
    @Override
    public void doAssign(AssginRoleVo assginRoleVo) {
        //根据用户id删除之前分配角色
        QueryWrapper<SysDimUserRole> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",assginRoleVo.getUserId());
        sysDimUserRoleMapper.delete(wrapper);
        //获取所有角色id，添加角色用户关系表
        //角色id列表
        List<String> roleIdList = assginRoleVo.getRoleIdList();
        for (String roleId:roleIdList) {
            SysDimUserRole userRole = new SysDimUserRole();
            userRole.setUserId(Long.parseLong(assginRoleVo.getUserId()));
            userRole.setRoleId(Long.parseLong(roleId));
            sysDimUserRoleMapper.insert(userRole);
        }
    }

    @Override
    public List<SysDimRole> getByCodeOrName(SysDimRole sysDimRole) {
        LambdaQueryWrapper<SysDimRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.and(sysDimRoleLambdaQueryWrapper -> sysDimRoleLambdaQueryWrapper.eq(StringUtils.isNotBlank(sysDimRole.getRoleCode()),SysDimRole::getRoleCode,sysDimRole.getRoleCode())
                .or()
                .eq(StringUtils.isNotBlank(sysDimRole.getRoleName()),SysDimRole::getRoleName,sysDimRole.getRoleName())).ne(StringUtils.isNotEmpty(sysDimRole.getId()),SysDimRole::getId,sysDimRole.getId());
        return this.baseMapper.selectList(queryWrapper);
    }
}
